//
//  QRViewController.m
//  PhyloHunter
//
//  Created by Sean Bramhall on 2013-06-07.
//
//	Programmer(s): Sean Bramhall, Chris Hamilton
//	Team Name: The Gigglers (Group 14)
//
/*  ToDo:
        -
        - Need to change the overlay of the scan window
        - Need scanner "back" button to link to main menu
        - Need recieve text data, parse number from bbmphylo# from QR code and append it to the server url
        - Need to access the server address and get the question and answer
        - question and answer need to be sent to the multichoice view
*/
//	Changes:
/*		July 26
        - added if statement so that QRscanner view only opens once
        July 25
        - moved the scanner into viewDidAppear method to align it with our design document after
        trying to put it in viewDidLoad
 
        July 14
        - implemented QR code reader and decoder using zbar implementation
 
        June 20
		- Now has an image that says "Coming Soon!" to show that this is a non-functional feature
			- But has navigation and functional 'Help' button
		- Changed the 'Help' layover (pop-up) wording and background image
 
		June 12
		- Modified the wording of the Help message
 
		June 11
		- Place a QR Code Image on the screen to show what the View is suppose to do
			- Temporary View
		- Created a 'Help' button and a dialog window
 */
//	Bugs: N/A
//
//	Task:	Is the View Controller for the 'Card Scanner'
//
//  Notes: - This file uses the compiler flag -fno-objc-arc to turn off arc for this file
//         - Modified from the zbar documentation at http://zbar.sourceforge.net/iphone/sdkdoc/
//


#import "QRViewController.h"




@interface QRViewController ()

@end

@implementation QRViewController

@synthesize helpMsg, helpOverLayBG, scan, server, cardid, cardnum, serveraddress, multi;

bool gotURL = NO;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.scan = YES;
    self.server = @"http://phylo.goodge.ca/question.ashx?cardid=";
	// Do any additional setup after loading the view.
}

- (void)viewDidAppear:(BOOL)animated
{
    
    
    if (self.scan == YES) // checks if QR scanner has run already
    {   
        ZBarReaderViewController *reader = [ZBarReaderViewController new];
    
        reader.readerDelegate = self;
        reader.supportedOrientationsMask = ZBarOrientationMaskAll;
    
    
        // disable all type of barcodes then enable only QR to increase decode efficency
        [reader.scanner setSymbology: 0
                              config: ZBAR_CFG_ENABLE
                                  to: 0];
        [reader.scanner setSymbology: ZBAR_QRCODE
                              config: ZBAR_CFG_ENABLE
                                  to: 1];
        reader.readerView.zoom = 1.0;
    
    
        // present and release the controller
        
        //***********************************
        //We must use this due to the use of an outdated library we are using not updated for iOS6
        [self presentModalViewController: reader
                            animated: YES];
        //***********************************
        [reader release];
        self.scan = NO;
    }
    //gotURL = YES;
    
    if (gotURL == YES)
    {
      //these comments are code used to bypass the camera we can not use in the simulator...do not delete
      /*  self.cardid = @"bbmphylo01";
        
        //remove the "bbmphylo"
        NSString *test = @"";
        
        NSString *tempCardNum = [self.cardid substringFromIndex:(8)];

        self.cardnum = [test stringByAppendingString:tempCardNum];
        
        serveraddress=[self.server stringByAppendingString:self.cardnum];
        */
      [self performSegueWithIdentifier:@"multisegue" sender:self];
    }
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
	
	if ([segue.identifier isEqualToString:@"multisegue"])
    {
		mutlichoiceViewController *destViewController = segue.destinationViewController;
		destViewController.serverURL = serveraddress;//@"http://phylo.goodge.ca/question.ashx?cardid=01";//
        destViewController.cardID = self.cardnum;
		
	}
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)backButton:(id)sender {
	[self dismissViewControllerAnimated:YES completion:Nil];
}

- (void) imagePickerController: (UIImagePickerController*) reader
 didFinishPickingMediaWithInfo: (NSDictionary*) info
{
    // get the decode results
    id<NSFastEnumeration> results =
    [info objectForKey: ZBarReaderControllerResults];
    ZBarSymbol *symbol = nil;
    for(symbol in results)
        break;
    
    // assigning QR data to a string
    self.cardid = symbol.data;
    
    //remove the "bbmphylo"
    NSString *test = @"";
    
    NSString *tempCardNum = [self.cardid substringFromIndex:(8)];
    
    self.cardnum = [test stringByAppendingString:tempCardNum];
    
    serveraddress=[self.server stringByAppendingString:self.cardnum];
    
    gotURL = YES;
    // dismiss the controller to prevent memory leaks
    //***********************************
    //We must use this due to the use of an outdated library we are using not updated for iOS6
    [reader dismissModalViewControllerAnimated: YES];
    //***********************************
}

 
- (void) dealloc
{
    [super dealloc];
}

- (BOOL) shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation) interfaceOrientation
{
    return(YES);
}


//The Help Message is defaulted to be Hidden
//		When the User Touches the Help button it will show the Help Message with its background
//		After Touching the Help button again, it will disappear
- (IBAction)HelpButton:(id)sender {
	if (helpMsg.hidden == NO) {
		helpMsg.hidden = YES;
		helpOverLayBG.hidden = YES;
	}
	else {
		helpMsg.hidden = NO;
		helpOverLayBG.hidden = NO;
	}
}
@end
